package com.hk.springsecurity03.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.session.FindByIndexNameSessionRepository;
import org.springframework.session.security.SpringSessionBackedSessionRegistry;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.CorsConfigurationSource;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;

import java.util.Arrays;

/**
 * @author by 何坤
 * @Classname SpringSecurityConfig
 * @Description TODO
 * @Date 2019/11/30 18:04
 */
@Configuration
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    private FindByIndexNameSessionRepository sessionRepository;


    /**
     * 完成了集群状态下的session会话共享
     * @param
     * @return org.springframework.session.security.SpringSessionBackedSessionRegistry
     * @date 2019/12/2 14:39
     * @author 何坤
     */
    /*@Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers("/admin/**").hasRole("ADMIN")
                .antMatchers("/user/**").hasRole("USER")
                .antMatchers("/app/**").permitAll()
                .anyRequest().authenticated()
                .and()
                .csrf().disable()
                .formLogin()
                .and()
                .sessionManagement()
                .maximumSessions(1)
                .sessionRegistry(springSessionBackedSessionRegistry());
    }*/


    /**
     * 启用CORS支持
     * @param
     * @param http
     * @return void
     * @date 2019/12/2 14:41
     * @author 何坤
     */
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers("/admin/**").hasRole("ADMIN")
                .antMatchers("/user/**").hasRole("USER")
                .antMatchers("/app/**").permitAll()
                .anyRequest().authenticated()
                .and()
                .cors()
                .and()
                .formLogin();
    }


    /**
     * CORS配置源
     * @param
     * @return org.springframework.web.cors.CorsConfigurationSource
     * @date 2019/12/2 14:42
     * @author 何坤
     */
    @Bean
    public CorsConfigurationSource corsConfigurationSource(){
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        // 允许从百度站点跨域和localhost:8080站点跨域
        corsConfiguration.setAllowedOrigins(Arrays.asList("http://localhost:8080", "https://www.baidu.com"));
        // 设置允许使用GET和POST方法
        corsConfiguration.setAllowedMethods(Arrays.asList("GET", "POST"));
        // 设置允许携带凭证
        corsConfiguration.setAllowCredentials(true);
        UrlBasedCorsConfigurationSource urlBasedCorsConfigurationSource = new UrlBasedCorsConfigurationSource();
        // 对所有的url生效
        urlBasedCorsConfigurationSource.registerCorsConfiguration("/**", corsConfiguration);
        return urlBasedCorsConfigurationSource;
    }


    @Bean
    public SpringSessionBackedSessionRegistry springSessionBackedSessionRegistry(){
        return new SpringSessionBackedSessionRegistry(this.sessionRepository);
    }

    @Bean
    public PasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }

}
